package com.miracle.common.utils;

import com.miracle.common.constant.Constants;
import com.miracle.common.utils.spring.SpringUtils;
import com.miracle.core.redis.RedisCache;
import com.miracle.system.domain.SysDictH;
import com.miracle.system.domain.SysDictL;
import com.miracle.system.mapper.SysDictHMapper;
import com.miracle.system.mapper.SysDictLMapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author ：miracle.cui
 * @description：TODO
 * @date ：2020/8/13 12:01
 */

public class DictUtils {

    private static SysDictHMapper dictHMapper;

    private static SysDictLMapper dictLMapper;

    private static List<SysDictH> dictHList = new ArrayList<SysDictH>();

    private static List<SysDictL> dictLList = new ArrayList<SysDictL>();

    /**
     * 设置字典缓存
     *
     * @param key 参数键
     * @param dictList 字典数据列表
     */
    public static void setDictCache(String key, List<SysDictL> dictList) {
        SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(key), dictList);
    }

    public static void setDictHList(){
        dictHList = dictHMapper.selectDictHList(null);
    }

    public static void setDictLList(){
        dictLList = dictLMapper.selectDictLList(null);
    }

    /**
     * 获取字典缓存
     *
     * @param key 参数键
     * @return dictList 字典数据列表
     */
    public static List<SysDictL> getDictCache(String key) {
        return SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
    }


    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    public static String getCacheKey(String configKey)
    {
        return Constants.SYS_DICT_KEY + configKey;
    }

    /**
     * 清空字典缓存
     */
    public static void clearDictCache()
    {
        Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(Constants.SYS_DICT_KEY + "*");
        SpringUtils.getBean(RedisCache.class).deleteObject(keys);
    }
}
